package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.utils.BeanUtilPro;
import com.sky.vo.DishPageItemVo;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public void insertWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.insert(dish);

        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {

            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.insert(flavors);
        }


    }

    @Override
    public PageResult<DishVO> selectBy(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dish = dishMapper.selectBy(dishPageQueryDTO);
        PageResult<DishVO> pageResult = new PageResult(dish.getTotal(), dish.getResult());
        return pageResult;
    }

    @Override
    public void delect(List<Long> ids) {
        dishMapper.delect(ids);
    }

    @Override
    public DishVO selectById(Long id) {
        Dish dish = dishMapper.selectById(id);
        DishVO dishVO = BeanUtilPro.getBeanByCopy(dish, DishVO.class);
        dishVO.setFlavors(dishFlavorMapper.getDFById(dish.getId()));
        return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {

//        修改菜品表基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);

//        删除原有的口味
        dishFlavorMapper.delect(dish);

//                重新插入口味的数据
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {

            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.insert(flavors);
        }
    }

    @Override
    public List<Dish> selectByCategoryId(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.selectByCategoryId(dish);

    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.selectByCategoryId(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getDFById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish=new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
//    public List<DishVO> listWithFlavor(Dish dish) {
//        List<Dish> dishList = dishMapper.selectByq(dish);
//
//        List<DishVO> dishVOList = new ArrayList<>();
//
//        for (Dish d : dishList) {
//            DishVO dishVO = new DishVO();
//            BeanUtils.copyProperties(d,dishVO);
//
//            //根据菜品id查询对应的口味
//            List<DishFlavor> flavors = dishFlavorMapper.getDFById(d.getId());
//
//            dishVO.setFlavors(flavors);
//            dishVOList.add(dishVO);
//        }
//
//        return dishVOList;
//    }

}
